/*
 * Copyright 2009 Vladimir Ritz Bossicard
 *
 * This file is part of OraDoc.
 *
 * OraDoc is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Version     : $Revision: 23 $
 * Last edit   : $Date: 2009-05-29 11:58:29 +0000 (Fri, 29 May 2009) $
 * Last editor : $Author: vbossica $
 */
package org.oradoc;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author Vladimir Ritz Bossicard
 */
public abstract class AbstractGeneratorApp {

    protected void start(final String[] args) {
        Options options = createOptions();
        try {
            CommandLineParser parser = new GnuParser();
            CommandLine line = parser.parse(options, args);

            if ((line.getOptions().length == 0 && areArgsRequired())|| line.hasOption("help") || line.hasOption("h")) {
                new HelpFormatter().printHelp(getClass().getName() + " [options]", options);
                return;
            }

            System.out.println("OraDoc " + new Version().getVersion());

            String name = line.getOptionValue("name");
            String version = line.getOptionValue("version");
            String connection = line.getOptionValue("connection");
            String username = line.getOptionValue("username");
            String password = line.getOptionValue("password");
            String outdir = line.getOptionValue("outdir");

            proceed(name, version, connection, username, password, outdir);
        } catch (ParseException e) {
            System.err.println("Execution failed : " + e.getMessage());
            new HelpFormatter().printHelp(getClass().getName() + " [options]", options);
        } catch (Exception e) {
            System.err.println("Execution failed : " + e.getMessage());
            e.printStackTrace(System.err);
        }
    }

    @SuppressWarnings("static-access")
    private Options createOptions() {
        boolean required = areArgsRequired();

        Options options = new Options();
        options.addOption(
            OptionBuilder.withLongOpt("name").hasArg().withArgName("string").withDescription("project name").isRequired(required).create('n'));
        options.addOption(
            OptionBuilder.withLongOpt("version").hasArg().withArgName("string").withDescription("project version").isRequired(required).create('v'));
        options.addOption(
            OptionBuilder.withLongOpt("connection").hasArg().withArgName("string").withDescription("connection string").isRequired(required).create('c'));
        options.addOption(
            OptionBuilder.withLongOpt("username").hasArg().withArgName("string").withDescription("db username").isRequired(required).create('u'));
        options.addOption(
            OptionBuilder.withLongOpt("password").hasArg().withArgName("string").withDescription("db password").isRequired(required).create('p'));
        options.addOption(
            OptionBuilder.withLongOpt("outdir").hasArg().withArgName("string").withDescription("output directory").isRequired(required).create('o'));
        options.addOption(new Option("h", "help", false, "prints this message"));

        return options;
    }

    protected abstract void proceed(String name, String version, String connection, String username, String password,
            String outdir) throws Exception;

    protected abstract boolean areArgsRequired();

}
